package com.doudizu.shoesonline.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.doudizu.shoesonline.model.entity.Orders;
import com.doudizu.shoesonline.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 15分钟后取消订单取消
 *
 * @author ln
 * 
 */
@Component
@Slf4j
public class CancelOrders {


    @Resource
    private OrdersService ordersService;

    /**
     * 每分钟执行一次
     */
    @Scheduled(fixedRate = 60 * 1000)
    public void run() {
        QueryWrapper<Orders> ordersQueryWrapper = new QueryWrapper<>();
        ordersQueryWrapper.eq("status",1);
        List<Orders> ordersList = ordersService.list(ordersQueryWrapper);
        for (Orders orders : ordersList) {
            Date fifteenMinutesAfter = new Date(orders.getCreateTime().getTime() + 15 * 60 * 1000);
            if ((new Date()).getTime() >= fifteenMinutesAfter.getTime()) {
                orders.setStatus(0);
                ordersService.updateById(orders);
            }
        }
    }
}
